package com.example.administrator.zhongyichou.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.example.administrator.zhongyichou.activity.ChildrenHelpActivity;
import com.example.administrator.zhongyichou.activity.DiseaseDetailsActivity;
import com.example.administrator.zhongyichou.activity.DreamDetailsActivity;
import com.example.administrator.zhongyichou.activity.DreamFundraisingActivity;
import com.example.administrator.zhongyichou.activity.ElderlyHelpActivity;
import com.example.administrator.zhongyichou.activity.FundraisingAdviserActivity;
import com.example.administrator.zhongyichou.activity.IllnessFundraisingActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.activity.MillionsHelpActivity;
import com.example.administrator.zhongyichou.activity.MutualAidActivity;
import com.example.administrator.zhongyichou.activity.YoungHelpActivity;
import com.example.administrator.zhongyichou.adapter.HomeFragmentAdapter;
import com.example.administrator.zhongyichou.adapter.HomeFragmentAdapter1;
import com.example.administrator.zhongyichou.adapter.HomeFragmentAdapter2;
import com.example.administrator.zhongyichou.base.BaseFragment;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.HomeProtocol;
import com.example.administrator.zhongyichou.view.FlyBanner;
import com.example.administrator.zhongyichou.view.MyListView;
import com.example.administrator.zhongyichou.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 第五版新加
 * Created by lichenyu on 2017/11/20.
 * 描述：首页
 */

public class HomeFragment extends BaseFragment<HomeProtocol> {

    @BindView(R.id.banner)
    FlyBanner banner;
    @BindView(R.id.ll_public_welfare)
    LinearLayout llPublicWelfare;
    @BindView(R.id.ll_illness_fundraising)
    LinearLayout llIllnessFundraising;
    @BindView(R.id.ll_dream_fundraising)
    LinearLayout llDreamFundraising;
    @BindView(R.id.ll_fundraising_adviser)
    LinearLayout llFundraisingAdviser;
    @BindView(R.id.listview)
    MyListView listview;
    @BindView(R.id.listview1)
    MyListView listview1;
    @BindView(R.id.listview2)
    MyListView listview2;

    private HomeFragmentAdapter adapter;
    private HomeFragmentAdapter1 adapter1;
    private HomeFragmentAdapter2 adapter2;
    private List<String> bannerList = new ArrayList<>();
    private HomeProtocol mGsonBaseProtocal;

    @Override
    protected void responseData(HomeProtocol mGsonBaseProtocal) {
        this.mGsonBaseProtocal = mGsonBaseProtocal;
        init();
        initMutual();
        initDisease();
        initDream();
    }

    //梦想互助
    private void initDream() {
        adapter2 = new HomeFragmentAdapter2(getContext(), mGsonBaseProtocal.getData().getDream_list());
        listview2.setAdapter(adapter2);
        listview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DreamDetailsActivity.class);
                intent.putExtra("info_id", mGsonBaseProtocal.getData().getDream_list().get(position).getInfo_id());
                startActivity(intent);
            }
        });
    }

    //大病互助
    private void initDisease() {
        adapter1 = new HomeFragmentAdapter1(getContext(), mGsonBaseProtocal.getData().getDisease_list());
        listview1.setAdapter(adapter1);
        listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DiseaseDetailsActivity.class);
                intent.putExtra("info_id", mGsonBaseProtocal.getData().getDisease_list().get(position).getInfo_id());
                startActivity(intent);
            }
        });
    }

    //互助筹款
    private void initMutual() {
        adapter = new HomeFragmentAdapter(getContext(), mGsonBaseProtocal.getData().getMutual_list());
        listview.setAdapter(adapter);
        adapter.setOnItemClick(new HomeFragmentAdapter.OnItemClick() {
            @Override
            public void onClick(int position) {
                switch (mGsonBaseProtocal.getData().getMutual_list().get(position).getType_id()) {
                    case "1"://百万终身大病互助行动
                        Intent intent = new Intent(getActivity(), MillionsHelpActivity.class);
                        startActivity(intent);
                        break;
                    case "2"://少儿大病互助行动
                        Intent intent2 = new Intent(getActivity(), ChildrenHelpActivity.class);
                        startActivity(intent2);
                        break;
                    case "3"://中青年大病互助行动
                        Intent intent1 = new Intent(getActivity(), YoungHelpActivity.class);
                        startActivity(intent1);
                        break;
                    case "4"://老年大病互助行动
                        Intent intent3 = new Intent(getActivity(), ElderlyHelpActivity.class);
                        startActivity(intent3);
                        break;
                }
            }
        });
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(HomeProtocol.class);
        requestHttpLoader.setParams(NetConfig.INDEX_LIST, new HashMap<String, String>());
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected View initContentView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_fragment, null);
        ButterKnife.bind(this, view);
        return view;
    }

    private void init() {
        bannerList.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1656942570,3532993932&fm=27&gp=0.jpg");
        bannerList.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1656942570,3532993932&fm=27&gp=0.jpg");
        bannerList.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1656942570,3532993932&fm=27&gp=0.jpg");
        bannerList.add("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1656942570,3532993932&fm=27&gp=0.jpg");
        banner.setImagesUrl(bannerList);
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    @OnClick({R.id.ll_public_welfare, R.id.ll_illness_fundraising, R.id.ll_dream_fundraising, R.id.ll_fundraising_adviser, R.id.ll_mutual, R.id.ll_disease, R.id.ll_dream})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_public_welfare://公益互筹
                Intent welfareIntent = new Intent(getActivity(), MutualAidActivity.class);
                startActivity(welfareIntent);
                break;
            case R.id.ll_illness_fundraising://大病互助
                Intent illnessIntent = new Intent(getActivity(), IllnessFundraisingActivity.class);
                startActivity(illnessIntent);
                break;
            case R.id.ll_dream_fundraising://梦想互助
                Intent dreamIntent = new Intent(getActivity(), DreamFundraisingActivity.class);
                startActivity(dreamIntent);
                break;
            case R.id.ll_mutual://公益互筹
                Intent welfareIntent1 = new Intent(getActivity(), MutualAidActivity.class);
                startActivity(welfareIntent1);
                break;
            case R.id.ll_disease://大病互助
                Intent illnessIntent1 = new Intent(getActivity(), IllnessFundraisingActivity.class);
                startActivity(illnessIntent1);
                break;
            case R.id.ll_dream://梦想互助
                Intent dreamIntent1 = new Intent(getActivity(), DreamFundraisingActivity.class);
                startActivity(dreamIntent1);
                break;
            case R.id.ll_fundraising_adviser://筹款顾问
                Intent fundraisingIntent = new Intent(getActivity(), FundraisingAdviserActivity.class);
                startActivity(fundraisingIntent);
                break;
        }
    }

}
